package netty.session;

import io.netty.channel.Channel;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class UserChannelSession {
    //userId->Channel
    private static Map<String, List<Channel>> multiSession=new HashMap<>();
    //channelId->userId
    private static Map<String,String> userChannelIdRelation=new HashMap<>();
    public static void putUserChannelIdRelation(String channelId,String userId){
        userChannelIdRelation.put(channelId,userId);
    }
    public static String getUserIdByChannelId(String channelId){
        return userChannelIdRelation.get(channelId);
    }
    public static void putMultiChannels(String userId,Channel channel){
        List<Channel> channels = getMultiSession(userId);
        if(CollectionUtils.isEmpty(channels)){
            channels=new ArrayList<>();
        }
        channels.add(channel);
        multiSession.put(userId,channels);
    }
    public static List<Channel> getMultiSession(String userId){
        return multiSession.get(userId);
    }
    public static void removeChannelList(String userId,String channelId){
        List<Channel> channels = getMultiSession(userId);
        if(CollectionUtils.isEmpty(channels)){
            return;
        }
        List<Channel> res=new ArrayList<>();
        for (int i=0;i<channels.size();i++) {
            if(!channels.get(i).id().asLongText().equals(channelId)){
                res.add(channels.get(i));
            }
        }
        multiSession.put(userId,res);
    }
}
